package com.kfm.order.service.impl;

import org.springframework.stereotype.Service;

import org.springframework.beans.factory.annotation.Autowired;

import com.kfm.common.domain.entity.ShoppingCart;
import com.kfm.order.mapper.ShoppingCartMapper;
import com.kfm.order.service.ShoppingCartService;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService{

    @Resource
    private ShoppingCartMapper shoppingCartMapper;

    @Override
    public int deleteByPrimaryKey(Integer cartId) {
        return shoppingCartMapper.deleteByPrimaryKey(cartId);
    }

    @Override
    public int insert(ShoppingCart record) {
        return shoppingCartMapper.insert(record);
    }

    @Override
    public int insertSelective(ShoppingCart record) {
        // 同一个商品购物车存在一次就可以，所有如果同一个用户存在某个物品，修改商品的数量就可以
        ShoppingCart cart = shoppingCartMapper.selectByMIdAndPId(record.getMemberId(), record.getProductId());
        if (cart != null) {
            cart.setQuantity(cart.getQuantity() + record.getQuantity());
            // 如果存在，修改数量就可以
            return shoppingCartMapper.updateByPrimaryKey(cart);
        }
        return shoppingCartMapper.insertSelective(record);
    }

    @Override
    public ShoppingCart selectByPrimaryKey(Integer cartId) {
        return shoppingCartMapper.selectByPrimaryKey(cartId);
    }

    @Override
    public int updateByPrimaryKeySelective(ShoppingCart record) {
        return shoppingCartMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(ShoppingCart record) {
        return shoppingCartMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<ShoppingCart> selectAll() {
        return shoppingCartMapper.selectAll();
    }

    @Override
    public List<ShoppingCart> selectByMId(Integer mid) {
        return shoppingCartMapper.selectByMId(mid);
    }

    @Override
    public int deleteMultipleElements(List<Integer> primaryKeyList) {
        return shoppingCartMapper.deleteMultipleElements(primaryKeyList);
    }

}
